<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%
	//统计日报  查询急诊数据
	//1 本部和分部的总量
	//2  按照日期查询
	//3 同期对比（去年的今天）
	//4 科室 排名和ghCount
	//5 专家 普通 ，急诊等类别进行分类汇总数量
	String date = input.getDesc("start_date","2018-01-28");//开始时间
	String endDa = input.getDesc("end_date","2018-01-28");//结束时间
	String yydm =  input.getDesc("yydm","01");//医院代码
	String ksdm =  input.getDesc("ksdm","-1");//科室代码
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	String sqlWhere = "";
	String sqlWhereCount = "";
	String sqlWhereDept = "";
	
	//查看所有急诊科
	
	if(!ksdm.equals("-1")){
		sqlWhere = " and ksdm in("+ksdm + ")";
		//sqlWhereCount = " and ejks_id in("+ksdm + ")";
		sqlWhereDept = " and id in("+ksdm + ")";
	}
	
	//存放返回结合
	Map result = new HashMap();
	/******************2 本部和分部的总量(某一时间点)*********************/
	
	
	
	String sqlCount = "select new map(count(*) as count) from mzjl where ksdm in (" + ksdm + ") and jlzt =0 and ghlb =1 and ghrq >'"+startDate+"' and ghrq <'"+endDate+"'";
	if(!ksdm.equals("-1")){
		sqlCount = "select new map(count(*) as count) from mzjl where ksdm in("+ksdm + ")"+"  and jlzt =0 and ghlb =1  and ghrq >'"+startDate+"' and ghrq <'"+endDate+"'";
	}
	
	List<Map> listCount2 = QBuilder.db("basedb").queryHql(sqlCount);
	
	//Integer count = (listCount1 == null || listCount1.size() == 0) ? 0 : Tools.getInt(listCount1.get(0).get("count").toString(),0);
	Integer count2 = (listCount2 == null || listCount2.size() == 0) ? 0 : Tools.getInt(listCount2.get(0).get("count").toString(),0);
	//result.put("currentCount",count);
	result.put("sumCount",count2);

	
	/******************4 科室 排名和ghCount*********************/
	String sqlQue = "select y.id as id ,y.name as name  ,case  when cont IS NULL  then 0 ELSE  g.[cont] END cont  from (select id ,name,yydm from YY_KSBMK where   yydm ='"+yydm+"' "+sqlWhereDept +"   ) y RIGHT JOIN (select g.ksdm , count(*) as cont  from  VW_GHZDK g  where ghrq >'"+ startDate +"' and ghrq <'" + endDate + "' and g.jlzt=0 and g.ghlb =1 GROUP BY ksdm  ) g  on g.ksdm = y.id where y.yydm ='"+yydm+"' ORDER BY g.cont desc ";
	if(yydm.equals("-1"))
	sqlQue = "select y.id as id ,y.name as name  ,case  when cont IS NULL  then 0 ELSE  g.[cont] END cont  from (select id ,name,yydm from YY_KSBMK where  1=1 "+sqlWhereDept +"   ) y RIGHT JOIN (select g.ksdm , count(*) as cont  from  VW_GHZDK g  where ghrq >'"+ startDate +"' and ghrq <'" + endDate + "' and g.jlzt=0 and g.ghlb =1 GROUP BY ksdm  ) g  on g.ksdm = y.id  ORDER BY g.cont desc ";
	
	StatelessSession ss =  DBTools.db("basedb").getSf().openStatelessSession();
       
	try {
		 ss.beginTransaction();
		 if(ksdm.equals("-1")){
			 SQLQuery sqlQuery = ss.createSQLQuery(sqlQue);
			 sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
			 List<Map> list = sqlQuery.list();
			 result.put("rank",list);
			 int countsum = 0;
			 for(Map map : list){
				 String con = map.get("cont") == null ? "0" : map.get("cont").toString();
				 countsum += Integer.parseInt(con);
			 }
			 result.put("sumCount",countsum);
		 }
	
		 
		 ss.getTransaction().commit();
		 ss.close();
		
	
    }catch (Exception e){
	    ss.getTransaction().commit();
		 ss.close();
    }
	
	Message msg = new Message();
	Map m1 = new HashMap();
	Map m2 = new HashMap();
	
	m2.put("data",result);

	
	msg.put("regEmergency",m2);
	out.println(msg);
%>